import { useFileStore } from "../stores/fileStore";
import type { FileSystemItem } from "../types/fileSystem";

export function useDragAndDrop() {
  const fileStore = useFileStore();

  const handleDragStart = (event: DragEvent, item: FileSystemItem) => {
    fileStore.draggedItem = item;
    if (event.dataTransfer) {
      event.dataTransfer.effectAllowed = "move";
      event.dataTransfer.setData("text/plain", item.name);

      // 创建自定义拖拽图像，避免默认黑边框
      const dragImage = document.createElement("div");
      dragImage.style.position = "absolute";
      dragImage.style.top = "-1000px";
      dragImage.style.background = "#e6f3ff";
      dragImage.style.border = "2px solid #409eff";
      dragImage.style.borderRadius = "4px";
      dragImage.style.padding = "8px 12px";
      dragImage.style.fontSize = "13px";
      dragImage.style.color = "#333";
      dragImage.style.boxShadow = "0 4px 12px rgba(64, 158, 255, 0.3)";
      dragImage.textContent = item.name;

      document.body.appendChild(dragImage);
      event.dataTransfer.setDragImage(dragImage, 10, 10);

      // 延迟移除临时元素
      setTimeout(() => {
        document.body.removeChild(dragImage);
      }, 0);
    }
  };

  const handleDragOver = (event: DragEvent, item: FileSystemItem) => {
    if (
      item.type === "folder" &&
      fileStore.draggedItem &&
      fileStore.draggedItem.name !== item.name
    ) {
      event.preventDefault();
      fileStore.dragOverItem = item;
    }
  };

  const handleDragLeave = (event: DragEvent, item: FileSystemItem) => {
    // 检查是否真的离开了目标元素（而不是进入子元素）
    const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
    const x = event.clientX;
    const y = event.clientY;

    if (x < rect.left || x > rect.right || y < rect.top || y > rect.bottom) {
      if (fileStore.dragOverItem?.name === item.name) {
        fileStore.dragOverItem = null;
      }
    }
  };

  const handleDrop = (event: DragEvent, targetItem?: FileSystemItem) => {
    event.preventDefault();

    // 只有在拖拽目标存在且是文件夹，并且当前确实在拖拽悬停状态时才执行移动
    if (
      fileStore.draggedItem &&
      targetItem &&
      targetItem.type === "folder" &&
      fileStore.dragOverItem?.name === targetItem.name
    ) {
      // 模拟移动文件到文件夹
      const draggedItem = fileStore.draggedItem;

      // 从当前目录移除
      const currentItems = fileStore.fileSystem[fileStore.currentPath];
      if (currentItems) {
        const index = currentItems.findIndex(
          item => item.name === draggedItem.name
        );
        if (index > -1) {
          currentItems.splice(index, 1);
        }
      }

      // 添加到目标目录
      const targetPath = fileStore.currentPath + "/" + targetItem.name;
      if (!fileStore.fileSystem[targetPath]) {
        fileStore.fileSystem[targetPath] = [];
      }
      fileStore.fileSystem[targetPath].push(draggedItem);
    }

    // 清理拖拽状态
    fileStore.dragOverItem = null;
    fileStore.draggedItem = null;
  };

  const handleDragEnd = () => {
    fileStore.draggedItem = null;
    fileStore.dragOverItem = null;
  };

  return {
    handleDragStart,
    handleDragOver,
    handleDragLeave,
    handleDrop,
    handleDragEnd,
  };
}
